package xin.javashare.algorithm;

import java.util.Scanner;

/**
 * 01背包问题
 * m为包的最大重量
 * n为商品的数量
 *
 * 参考：http://www.cnblogs.com/lfeng1205/p/5981198.html
 */
public class Beibao {
    public static void main(String[] agrs) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            int w[] = new int[n];
            int v[] = new int[n];
            //w[i]为第i+1个物品的重量
            for (int i = 0; i < n; i++) {
                w[i] = sc.nextInt();
            }
            //v[i]为第i+1个物品的价值
            for (int j = 0; j < n; j++) {
                v[j] = sc.nextInt();
            }
            //s[i][j]为前i个物品放入重量为j的背包中的最大价值
            int s[][] = new int[n + 1][m + 1];
            for (int i = 0; i < n + 1; i++) {
                s[i][0] = 0;
            }
            for (int j = 0; j < m + 1; j++) {
                s[0][j] = 0;
            }
            for (int i = 1; i < n + 1; i++) {
                for (int j = 1; j < m + 1; j++) {
                    if (w[i - 1] <= j) {
                        if (s[i - 1][j] < (s[i - 1][j - w[i - 1]] + v[i - 1])) {
                            s[i][j] = s[i - 1][j - w[i - 1]] + v[i - 1];
                        }
                        else {
                            s[i][j] = s[i - 1][j];
                        }
                    }
                    else {
                        s[i][j] = s[i - 1][j];
                    }
                }
            }
            for (int i = 1; i < n + 1; i++) {
                for (int j = 1; j < m + 1; j++) {
                    System.out.print(s[i][j] + "\t");
                    if (j == m) {
                        System.out.println();
                    }
                }
            }
        }
    }
}